package com.jeethink.erp.shop.controller;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.jeethink.common.core.domain.entity.SysUser;
import com.jeethink.common.core.domain.entity.TbShop;
import com.jeethink.common.core.domain.model.LoginUser;
import com.jeethink.common.core.page.TableDataInfo;
import com.jeethink.common.utils.ServletUtils;
import com.jeethink.common.utils.spring.SpringUtils;
import com.jeethink.erp.shop.domain.TbShopVo;
import com.jeethink.framework.web.service.TokenService;
import com.jeethink.system.service.ISysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.sf.json.JSONObject;
import org.aspectj.weaver.loadtime.Aj;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.jeethink.common.annotation.Log;
import com.jeethink.common.core.controller.BaseController;
import com.jeethink.common.core.domain.AjaxResult;
import com.jeethink.common.enums.BusinessType;
import com.jeethink.erp.shop.service.ITbShopService;
import com.jeethink.common.utils.poi.ExcelUtil;

/**
 * 店铺树结构Controller
 * 
 * @author ypr
 * @date 2021-05-26
 */
@Api("店铺信息")
@RestController
@RequestMapping("/erp/shop")
public class TbShopController extends BaseController
{
    @Autowired
    private ITbShopService tbShopService;

    @Autowired
    private ISysUserService userService;

    /**
     * 查询店铺树结构列表
     */
    @ApiOperation("获取店铺信息")
    @GetMapping("/list")
    public AjaxResult list(TbShop tbShop)
    {
        List<TbShop> tbShops = tbShopService.selectTbShopList(tbShop);
        return AjaxResult.success(tbShops);
    }

    /**
     * 导出店铺树结构列表
     */
    @PreAuthorize("@ss.hasPermi('erp:shop:export')")
    @Log(title = "店铺树结构", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(TbShop tbShop)
    {
        List<TbShop> list = tbShopService.selectTbShopList(tbShop);
        ExcelUtil<TbShop> util = new ExcelUtil<TbShop>(TbShop.class);
        return util.exportExcel(list, "shop");
    }

    /**
     * 获取店铺树结构详细信息
     */
    @PreAuthorize("@ss.hasPermi('erp:shop:query')")
    @GetMapping(value = "/{shopId}")
    public AjaxResult getInfo(@PathVariable("shopId") Long shopId)
    {
        return AjaxResult.success(tbShopService.selectTbShopById(shopId));
    }

    /**
     * 新增店铺树结构
     */
    @PreAuthorize("@ss.hasPermi('erp:shop:add')")
    @Log(title = "店铺树结构", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TbShop tbShop)
    {
        return toAjax(tbShopService.insertTbShop(tbShop));
    }

    /**
     * 修改店铺树结构
     */
    @PreAuthorize("@ss.hasPermi('erp:shop:edit')")
    @Log(title = "店铺树结构", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TbShop tbShop)
    {
        return toAjax(tbShopService.updateTbShop(tbShop));
    }

    /**
     * 删除店铺树结构
     */
    @PreAuthorize("@ss.hasPermi('erp:shop:remove')")
    @Log(title = "店铺树结构", businessType = BusinessType.DELETE)
	@DeleteMapping("/{shopIds}")
    public AjaxResult remove(@PathVariable Long[] shopIds)
    {
        return toAjax(tbShopService.deleteTbShopByIds(shopIds));
    }

    /**
     * 获取题库下拉树列表
     */
    @GetMapping("/treeselect")
    public AjaxResult treeselect(TbShop tbShop)
    {
        List<TbShop> list = tbShopService.selectTbShopList(tbShop);
        return AjaxResult.success(tbShopService.buildShopTreeSelect(list));
    }

    @GetMapping("/userTreeselect")
    public AjaxResult userTreeselect(){
        List<SysUser> userList = userService.selectUserList(new SysUser());
        return AjaxResult.success(userList);
    }

    /**
     * 我的店铺  根据当前登录用户查询我的店铺
     * @param tbShop
     * @return
     */
    @GetMapping("/getShopList")
    public TableDataInfo getShopList(TbShop tbShop)
    {
        //获取当前登录用户
        LoginUser loginUser = SpringUtils.getBean(TokenService.class).getLoginUser(ServletUtils.getRequest());
        SysUser user = loginUser.getUser();
        tbShop.setShopId(user.getShopId());
        List<TbShop> list = tbShopService.selectTbShopList(tbShop);
        return getDataTable(list);
    }

    @GetMapping("/findShopPile")
    public TableDataInfo findShopPile(TbShop tbShop){
        startPage();
        List<TbShop> findShopPileList = tbShopService.selectTbShopList(tbShop);
        return getDataTable(findShopPileList);
    }

    /**
     * index chart获取当天销售金额方法
     * @return
     */
    @GetMapping("/findShopChatList")
    public AjaxResult findShopChatList(){
        TbShop tbShop = new TbShop();
        LoginUser loginUser = SpringUtils.getBean(TokenService.class).getLoginUser(ServletUtils.getRequest());

        TbShop shop = tbShopService.selectTbShopById(loginUser.getUser().getShopId());
        //如果店铺父级id 为0 代表为一级店铺
        if(shop.getParentId() == 0){
            tbShop.setShopId(null);
        }else {
            tbShop.setShopId(loginUser.getUser().getShopId());
        }
        List<TbShop> tbShopList = tbShopService.selectShopChartList(tbShop);
        Map<String,Object> map = new HashMap();
        List<String> nameDateList = new ArrayList<>();
        List<String> valueDateList = new ArrayList<>();
        for (int i=0; i< tbShopList.size(); i++){
            nameDateList.add(tbShopList.get(i).getShopName());
            valueDateList.add(String.valueOf(tbShopList.get(i).getPrice()));
        }
        String[] nameDate = nameDateList.toArray(new String[nameDateList.size()]);
        String[] valueDate = valueDateList.toArray(new String[valueDateList.size()]);

        map.put("nameDate",nameDate);
        map.put("valueDate",valueDate);
        return AjaxResult.success(map);
    }

    /**
     * index chart根据当前月份获取柱状图方法 ypr
     * @return
     */
    @GetMapping("/findShopMonthList")
    public AjaxResult selectMonthList(){
        TbShop tbShop = new TbShop();
        LoginUser loginUser = SpringUtils.getBean(TokenService.class).getLoginUser(ServletUtils.getRequest());

        TbShop shop = tbShopService.selectTbShopById(loginUser.getUser().getShopId());
        //如果店铺父级id 为0 代表为一级店铺
        if(shop.getParentId() == 0){
            tbShop.setShopId(null);
        }else {
            tbShop.setShopId(loginUser.getUser().getShopId());
        }

        LocalDate now = LocalDate.now();
        tbShop.setSaleMonth(String.valueOf(now.getMonthValue()));
        List<TbShop> findMonthList = tbShopService.selectMonthList(tbShop);
        Map<String,Object> map = new HashMap();
        List<String> nameDateList = new ArrayList<>();
        List<String> valueDateList = new ArrayList<>();
        for (int i=0; i< findMonthList.size(); i++){
            nameDateList.add(findMonthList.get(i).getShopName());
            valueDateList.add(String.valueOf(findMonthList.get(i).getPrice()));
        }
        String[] nameDate = nameDateList.toArray(new String[nameDateList.size()]);
        String[] valueDate = valueDateList.toArray(new String[valueDateList.size()]);

        map.put("nameDate",nameDate);
        map.put("valueDate",valueDate);
        return AjaxResult.success(map);
    }

    /**
     * index chart获取饼图数据方法 ypr
     * @return
     */
    @GetMapping("/findCommodityChart")
    public AjaxResult findCommodityChart(){
        TbShopVo tbShopVo = new TbShopVo();
        LoginUser loginUser = SpringUtils.getBean(TokenService.class).getLoginUser(ServletUtils.getRequest());

        TbShop tbShop = tbShopService.selectTbShopById(loginUser.getUser().getShopId());
        //如果店铺父级id 为0 代表为一级店铺
        if(tbShop.getParentId() == 0){
            tbShopVo.setShopId(null);
        }else {
            tbShopVo.setShopId(loginUser.getUser().getShopId());
        }

        List<TbShopVo> tbShopList = tbShopService.selectVoChart(tbShopVo);
        Map<String,Object> map = new HashMap();
        for (int i = 0; i<tbShopList.size();i++){
            //商品对应销量
            String[] numBer = tbShopList.get(i).getSumprice().split(",");
            //商品名称
            String[] valueString = tbShopList.get(i).getCommodityName().split(",");
            int[] intArr = new int[numBer.length];
            int num = 0;
            Map valueMap = new HashMap();
            //循环将商品名称与商品销量放置到map中
            for (int k = 0; k < numBer.length; k++) {
                intArr[k] = Integer.parseInt(numBer[k]);
                num+= intArr[k];

                valueMap.put(valueString[k],numBer[k]);
            }
            //将map数据 转换为 json对象传至前台
            JSONObject jsonObject = JSONObject.fromObject(valueMap);
            tbShopList.get(i).setJsonObject(jsonObject);


            String[] vueRadius = "0,30%".split(",");
            tbShopList.get(i).setVueRadius(vueRadius);
            String val = "";
            if(i == 0){
                tbShopList.get(i).setVueLeft("15%");
                tbShopList.get(i).setNum(String.valueOf(num));
                val = "15%,25%";
                String[] vueCenter = val.split(",");
                tbShopList.get(i).setVueCenter(vueCenter);
            }else if(i == 1){
                tbShopList.get(i).setVueLeft("45%");
                tbShopList.get(i).setNum(String.valueOf(num));
                val = "45%,25%";
                String[] vueCenter = val.split(",");
                tbShopList.get(i).setVueCenter(vueCenter);
            }else if(i == 2){
                tbShopList.get(i).setVueLeft("75%");
                tbShopList.get(i).setNum(String.valueOf(num));
                val = "75%,25%";
                String[] vueCenter = val.split(",");
                tbShopList.get(i).setVueCenter(vueCenter);
            }else if(i == 3){
                tbShopList.get(i).setVueLeft("15%");
                tbShopList.get(i).setVueTop("50%");
                tbShopList.get(i).setNum(String.valueOf(num));
                val = "15%,75%";
                String[] vueCenter = val.split(",");
                tbShopList.get(i).setVueCenter(vueCenter);
            }else if(i == 4){
                tbShopList.get(i).setVueLeft("45%");
                tbShopList.get(i).setVueTop("50%");
                tbShopList.get(i).setNum(String.valueOf(num));
                val = "45%,75%";
                String[] vueCenter = val.split(",");
                tbShopList.get(i).setVueCenter(vueCenter);
            }else if(i == 5){
                tbShopList.get(i).setVueLeft("75%");
                tbShopList.get(i).setVueTop("50%");
                tbShopList.get(i).setNum(String.valueOf(num));
                val = "75%,75%";
                String[] vueCenter = val.split(",");
                tbShopList.get(i).setVueCenter(vueCenter);
            }
        }
        map.put("dataList2",tbShopList);
        return AjaxResult.success(map);
    }


    @GetMapping("/findCountSize")
    public AjaxResult findCountSize(){
        TbShopVo tbShopVo = new TbShopVo();
        LoginUser loginUser = SpringUtils.getBean(TokenService.class).getLoginUser(ServletUtils.getRequest());

        TbShop tbShop = tbShopService.selectTbShopById(loginUser.getUser().getShopId());
        //如果店铺父级id 为0 代表为一级店铺
        if(tbShop.getParentId() == 0){
            tbShopVo.setShopId(null);
        }else {
            tbShopVo.setShopId(loginUser.getUser().getShopId());
        }

        TbShopVo vo = tbShopService.findCountSize(tbShopVo);
        return AjaxResult.success(vo);
    }


}
